"use strict";

module.exports = (app) => {
  const { STRING, INTEGER, DECIMAL, TEXT, BOOLEAN, DATE, JSON } = app.Sequelize;

  const Product = app.model.define("product", {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING(100),
      allowNull: false,
      comment: "商品名称",
    },
    category_id: {
      type: INTEGER,
      allowNull: false,
      comment: "分类ID",
    },
    brand_id: {
      type: INTEGER,
      comment: "品牌ID",
    },
    specs: {
      type: JSON,
      defaultValue: [],
    },
    skus: {
      type: JSON,
      defaultValue: [],
    },
    price: {
      type: DECIMAL(10, 2),
      allowNull: false,
      comment: "价格",
    },
    // cost_price: {
    //   type: DECIMAL(10, 2),
    //   comment: "成本价",
    // },
    original_price: {
      type: DECIMAL(10, 2),
      comment: "原价",
    },
    stock: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "库存",
    },
    sales: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "销量",
    },
    weight: {
      type: DECIMAL(10, 3),
      comment: "重量(kg)",
    },
    volume: {
      type: DECIMAL(10, 3),
      comment: "体积(m³)",
    },
    // unit: {
    //   type: STRING(20),
    //   comment: "单位",
    // },
    cover_image: {
      type: STRING(255),
      comment: "封面图",
    },
    description: {
      type: TEXT,
      comment: "商品描述",
    },
    content: {
      type: TEXT,
      comment: "商品详情内容",
    },
    status: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: true,
      comment: "状态(1:上架,0:下架)",
    },
    is_hot: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: "是否热销",
    },
    is_new: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: "是否新品",
    },
    is_recommend: {
      type: BOOLEAN,
      allowNull: false,
      defaultValue: false,
      comment: "是否推荐",
    },
    sort_order: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "排序",
    },
    created_at: DATE,
    updated_at: DATE,
  });

  // 关联关系
  Product.associate = function () {
    // 商品与分类：多对一关系
    app.model.Product.belongsTo(app.model.Category, {
      foreignKey: "category_id",
    });

    // 商品与品牌：多对一关系
    app.model.Product.belongsTo(app.model.Brand, { foreignKey: "brand_id" });

    // 商品与图片：一对多关系
    app.model.Product.hasMany(app.model.ProductImage, {
      foreignKey: "product_id",
    });

    // 商品与规格：一对多关系
    // app.model.Product.hasMany(app.model.ProductSpec, {
    //   foreignKey: "product_id",
    // });
  };

  // 获取SKU信息
  Product.prototype.getSkuById = function (skuId) {
    return this.skus.find((sku) => sku.id === skuId);
  };

  // 获取SKU库存
  Product.prototype.getSkuStock = function (skuId) {
    const sku = this.getSkuById(skuId);
    return sku ? sku.stock : 0;
  };

  // 更新SKU库存
  Product.prototype.updateSkuStock = function (skuId, stock) {
    const skuIndex = this.skus.findIndex((sku) => sku.id === skuId);
    if (skuIndex !== -1) {
      this.skus[skuIndex].stock = stock;
      return true;
    }
    return false;
  };

  // 计算商品总库存
  Product.prototype.calculateTotalStock = function () {
    return this.skus.reduce((total, sku) => total + sku.stock, 0);
  };

  return Product;
};
